{% extends "student_portal/course_base.html" %}
{% load student_portal_extras %}

{% block extrahead %}
<script type="text/javascript">
function deleteSubmission(submissionId) {
    if (confirm("Are you sure you want to delete this Submission?")) {
        var submission;
        submission = "deleteSubmission-" + submissionId;
        document.getElementById(submission).submit();
    }
}
</script>
{% endblock %}

{% block content %}

{# Page Heading #}
<h2>{{ course.name }} - Assignments</h2>

{# Detail Information #}
<div>
    Manage student course workload with this interactive Assignments interface.
    On this page you have the ability to submit homework, check progress, and view Grader feedback.<br /><br />
    <b>Please Note:</b> Download links are time sensitive for security, so you may need to reload the page for a fresh link.<br /><br />
</div>

{# Print Messages Stack #}
{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

{# List Assignments #}
<h3>Course Assignments</h3>
{% if assignment_list.object_list %}

{# Configure Table #}
<table summary="Current Assignments" class="assignments" cellspacing="0">
<thead>
    <tr>
        <th>Name</th>
        <th>Due Date</th>
        <th>Grading</th>
    </tr>
</thead>

{####################################################}
{# Cycle through the course assignment data stored in assignment_list #}
{# assignment_list is a complete list of elements that look like this:  #}
{# [0=Assignment, 1=Submission, 2=Grade, 3=DueDateOverride] #}
{####################################################}
{% for assignment_data in assignment_list.object_list %}

{# First Row Assignment Header #}
<tr class={{ assignment_data|getTrClass|safe }}>
    {# Assignment Name #}
    <td><b>{{ assignment_data.0.name }}</b></td>

    {# Assignment Due Date #}
    <td><b>
    {% if assignment_data.3 %}
        {{ enrollment.start_date|adjustDateWeeks:assignment_data.3.weeks_after|adjustDateDays:assignment_data.3.days_after|date:"M jS Y" }}
    {% else %}
        {% if assignment_data.0.apply_due_date %}
            {{ enrollment.start_date|adjustDateWeeks:assignment_data.0.weeks_after|adjustDateDays:assignment_data.0.days_after|date:"M jS Y" }}
        {% else %}
            None Listed
        {% endif %}
    {% endif %}
   </b></td>

   {# Assignment Grading #}
   <td><b>
        {# Display Grade -> points earned #}
        {% if assignment_data.2 %}
            {{ assignment_data.2.points_earned }} /
        {% else %}
            -- /
        {% endif %}

        {# Display Assignment -> points possible #}
        {% if assignment_data.0.display_points_possible %}
            {{ assignment_data.0.points_possible }}
        {% else %}
            --
        {% endif %}
    </b></td>
</tr>

{# Second row: Assignment Details #}
<tr class={{ assignment_data|getTrClass|safe }}>
    <td><em><b>Details:</b></em></td>
    <td class="assignmentDesc" colspan="2"> {{ assignment_data.0.details }} </td>
</tr>

{# Third row for Assignment -> provided_files #}
{% if assignment_data.0.provided_files %}
<tr class={{ assignment_data|getTrClass|safe }}>
    <td> <em><b>Provided Files:</b></em> </td>
    <td>
        <img class="icon" src="{{ STATIC_URL }}icon_download_small.png" alt="Download" />
        {% if assignment_data.0.provided_files %}
            <b><a href="{{ assignment_data.0.provided_files.url|safe }}">Download</a></b>
        {% endif %}
    </td>
    <td></td>
</tr>
{% endif %}

{# Finally, Add row for student Submissions #}
{% if assignment_data.1 %} {# Existing Submissions ... #}
<tr class={{ assignment_data|getTrClass|safe }}>   
    <td colspan="3"> <em><b>Submitted:</b></em> {{ assignment_data.1.date|date:"M jS Y" }} -
        {% if assignment_data.1.on_time %}
            <b>On Time</b>
        {% else %}
            <b>Late</b>
        {% endif %}
        <br />
        <img class="icon" src="{{ STATIC_URL }}icon_download_small.png" alt="Download" />
        <em><a href="{{ assignment_data.1.file.url|safe }}">Download Your Submission</a></em> <br />

        {# Add delete link when 0.) Submission Exists 1.) Deadline not passed 2.) student not received Grade #}
        {% if not assignment_data.2 %} {# When no Grade ... #}
            {% if assignment_data.3 %} {# Verify against any existing DueDateOverride. #}
                {% if not assignment_data.0.apply_due_date or not enrollment.start_date|adjustDateWeeks:assignment_data.3.weeks_after|adjustDateDays:assignment_data.3.days_after|isLateNow %}
                    <form id="deleteSubmission-{{ assignment_data.1.id }}" method="post" action="delsubmission/{{ assignment_data.1.id }}/">{% csrf_token %}
                    <div><img class="icon" src="{{ STATIC_URL }}icon_delete_small.png" alt="Delete Submission" />
                        <em><a href="javascript:deleteSubmission({{ assignment_data.1.id }})">
                            Delete Your Submission</a></em><br /> </div>
                    </form>
                {% endif %}
            {% else %} {# Otherwise Verify against any existing Assignment default DueDate. #}
                {% if not assignment_data.0.apply_due_date or not enrollment.start_date|adjustDateWeeks:assignment_data.0.weeks_after|adjustDateDays:assignment_data.0.days_after|isLateNow %}
                    <form id="deleteSubmission-{{ assignment_data.1.id }}" method="post" action="delsubmission/{{ assignment_data.1.id }}/">{% csrf_token %}
                    <div><img class="icon" src="{{ STATIC_URL }}icon_delete_small.png" alt="Delete Submission" />
                        <em><a href="javascript:deleteSubmission({{ assignment_data.1.id }})">
                            Delete Your Submission</a></em><br /> </div>
                    </form>
                {% endif %}
            {% endif %}
        {% endif %}
    </td>
</tr>
{% else %} {# Student does not have a Submission ... #}
    {# When online submissions are disabled for the Assignment: #}
    {% if assignment_data.0.disable_submissions %}
    <tr class={{ assignment_data|getTrClass|safe }}>
        <td> <em><b>Submit:</b></em> </td>
        <td colspan="2"> <b>Online Submissions Disabled for this Assignment.</b></td>
    </tr>
    {% else %} {# Online Submissions are Enabled: #}
    {% if not assignment_data.2 %} {# Student does not already have Grade: #}
    <tr class={{ assignment_data|getTrClass|safe }}>
    <td> <em><b>Submit:</b></em> </td> <td colspan="2">
    {% if assignment_data.3 %} {# When DueDateOverride exists: #}
        {% if assignment_data.3.allow_late %} {# Check if it allows late submissions. #}
            {{ assignment_data.0.id|printSubmitFormStart|safe }}{% csrf_token %}{{ form|printSubmitFormEnd|safe }}
        {% else %} {# Otherwise Verify Time Stamps. #}
        {% if enrollment.start_date|adjustDateWeeks:assignment_data.3.weeks_after|adjustDateDays:assignment_data.3.days_after|isLateNow %}
            <b>Late Submissions are Disabled.</b>
        {% else %}
            {{ assignment_data.0.id|printSubmitFormStart|safe }}{% csrf_token %}{{ form|printSubmitFormEnd|safe }}
            <b>Late Submissions Not Allowed!</b>
        {% endif %}
        {% endif %}
    {% else %}{% if assignment_data.0.apply_due_date %} {# When Assignment due date exists: #}
        {% if assignment_data.0.allow_late %} {# Assignment can be submitted Late: #}
            {{ assignment_data.0.id|printSubmitFormStart|safe }}{% csrf_token %}{{ form|printSubmitFormEnd|safe }}
        {% else %} {# Late Submissions not allowed, check time stamps: #}
        {% if enrollment.start_date|adjustDateWeeks:assignment_data.0.weeks_after|adjustDateDays:assignment_data.0.days_after|isLateNow %}
            <b>Late Submissions are Disabled.</b>
        {% else %}
            {{ assignment_data.0.id|printSubmitFormStart|safe }}{% csrf_token %}{{ form|printSubmitFormEnd|safe }}
            <b>Late Submissions Not Allowed!</b>
        {% endif %}
        {% endif %}
    {% else %} {# No Assignment Due Date in place: #}
        {{ assignment_data.0.id|printSubmitFormStart|safe }}{% csrf_token %}{{ form|printSubmitFormEnd|safe }}
    {% endif %}
    {% endif %} {# End block: "When DueDateOverride exists" #}
    </td></tr>
    {% endif %} {# End block: "Student does not already have Grade." #}
    {% endif %}
{% endif %} {# End Block: "Student does not have a Submission ..." #}

{# Finally add rows for Grader Comments and returned files #} 
{% if assignment_data.2 %}
    {# Grader Comments #}
    <tr class={{ assignment_data|getTrClass|safe }}>
    <td> <em><b>Grader Comments:</b></em> </td>
    {% if assignment_data.2.comments %}
        <td class="assignmentDesc" colspan="2"> {{ assignment_data.2.comments }} </td>
    {% else %}
        <td class="assignmentDesc" colspan="2"><em>No Grader Comments Listed.</em></td>
    {% endif %}
    </tr>
    {# Returned Files #}
    {% if assignment_data.2.returned_files %}
        <tr class={{ assignment_data|getTrClass|safe }}>
            <td> <em><b>Returned File:</b></em> </td>
            <td>
                <img class="icon" src="{{ STATIC_URL }}icon_download_small.png" alt="Download" />
                <b><a href="{{ assignment_data.2.returned_files.url|safe }}">Download</a></b> <br />
            </td>
        <td></td></tr>
   {% endif %}
{% endif %}

{# Row Separator #}
{% if not forloop.last %}
   <tr class="assignmentBlank"><td colspan="3"></td></tr>
{% endif %}

{% endfor %}
</table>

<div class="pagination">
    <span class="step-links">
        {% if assignment_list.has_previous %}
            <a href="?page={{ assignment_list.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ assignment_list.number }} of {{ assignment_list.paginator.num_pages }}.
        </span>

        {% if assignment_list.has_next %}
            <a href="?page={{ assignment_list.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

{% else %}
<i><h4>No Assignments Listed.</h4></i>
{% endif %}

{% endblock %}

